/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.domain.ZhsqHistory;
import me.zhengjie.business.repository.AdlZhsqRepository;
import me.zhengjie.business.repository.ZhsqRepository;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.ZhsqHistoryRepository;
import me.zhengjie.business.service.ZhsqHistoryService;
import me.zhengjie.business.service.dto.ZhsqHistoryDto;
import me.zhengjie.business.service.dto.ZhsqHistoryQueryCriteria;
import me.zhengjie.business.service.mapstruct.ZhsqHistoryMapper;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2022-07-24
**/
@Service
@RequiredArgsConstructor
public class ZhsqHistoryServiceImpl implements ZhsqHistoryService {

    private final ZhsqHistoryRepository zhsqHistoryRepository;
    private final ZhsqHistoryMapper zhsqHistoryMapper;

    private final ZhsqRepository zhsqRepository;
    private final AdlZhsqRepository adlZhsqRepository;

    @Override
    public Page<ZhsqHistory> queryAll(ZhsqHistoryQueryCriteria criteria, Pageable pageable){
        Page<ZhsqHistory> page = zhsqHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return page;
    }

    @Override
    public List<ZhsqHistory> queryList(ZhsqHistoryQueryCriteria criteria){
    /*    Sort sort = Sort.by(new Sort.Order(Sort.Direction.DESC,"lrsj"));
        return zhsqHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder), sort);*/
        Zhsq zhsq = zhsqRepository.findById(criteria.getZhsqId()).get();
        String c1 = adlZhsqRepository.queryC1(criteria.getZhsqId());
        String c2 = adlZhsqRepository.queryC2(criteria.getZhsqId());
        String c3 = adlZhsqRepository.queryC3(criteria.getZhsqId());
        Map<String, String> snMap = GetSndj.analyse(Integer.parseInt(c1),Integer.parseInt(c2),Integer.parseInt(c3), Integer.parseInt(zhsq.getBisZhsqNl()));
        String sndj = snMap.get(GetSndj.SNDJ);
        String dyqx = snMap.get(GetSndj.DYQX);
        List<ZhsqHistory> list = new ArrayList<>();
        ZhsqHistory zhsqHistory = new ZhsqHistory();
        zhsqHistory.setLrrxm("C1:"+c1+"分/C2:"+c2+"分/C3:"+c3+"分");
        zhsqHistory.setResult(sndj);
        zhsqHistory.setBz(dyqx+"个月");
        list.add(zhsqHistory);
        return list;
    }
    @Override
    public List<ZhsqHistoryDto> queryAll(ZhsqHistoryQueryCriteria criteria){
        return zhsqHistoryMapper.toDto(zhsqHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public ZhsqHistory queryLastData(Long id){

        return zhsqHistoryRepository.queryLastData(id);
    }

    @Override
    public ZhsqHistory queryBh11(Long id) {
        return zhsqHistoryRepository.queryBh11(id);
    }

    @Override
    @Transactional
    public ZhsqHistoryDto findById(String id) {
        ZhsqHistory zhsqHistory = zhsqHistoryRepository.findById(id).orElseGet(ZhsqHistory::new);
        ValidationUtil.isNull(zhsqHistory.getId(),"ZhsqHistory","id",id);
        return zhsqHistoryMapper.toDto(zhsqHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhsqHistoryDto create(ZhsqHistory resources) {
        resources.setId(IdUtil.simpleUUID());
        return zhsqHistoryMapper.toDto(zhsqHistoryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhsqHistory resources) {
        ZhsqHistory zhsqHistory = zhsqHistoryRepository.findById(resources.getId()).orElseGet(ZhsqHistory::new);
        ValidationUtil.isNull( zhsqHistory.getId(),"ZhsqHistory","id",resources.getId());
        zhsqHistory.copy(resources);
        zhsqHistoryRepository.save(zhsqHistory);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            zhsqHistoryRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhsqHistoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhsqHistoryDto zhsqHistory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<String> getQzUser(String zhsqId) {
        return zhsqHistoryRepository.getQzUser(zhsqId);
    }
}
